package com.itheima.ai.config;

import com.itheima.ai.constant.SystemConstants;
import com.itheima.ai.tools.courseTools;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.observation.conventions.VectorStoreProvider;
import org.springframework.ai.ollama.OllamaChatModel;

import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;


@Configuration
public class commonConfig {

    @Autowired
    private ChatMemoryRepository inMemoryChatMemoryRepository;
    @Autowired
    private courseTools courseTools;

    @Bean
    public ChatMemory getChatMemory() {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(inMemoryChatMemoryRepository)
                .maxMessages(10)
                .build();
    }


    @Bean
    public ChatClient chatClient(OpenAiChatModel openAiChatModel){
        return ChatClient
                .builder(openAiChatModel)
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
                .defaultSystem("你是一个热心可爱的智能助手，你的名字叫咪西，请以咪西的名义可爱地回答问题")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(getChatMemory()).build()
                )
                .build();
    }
    @Bean
    public ChatClient OpenAIChatClient(OpenAiChatModel openAiChatModel){
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(getChatMemory()).build()
                )
                .build();
    }

    @Bean
    public ChatClient ServiceChatClient(OpenAiChatModel openAiChatModel){
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(getChatMemory()).build()
                )
                .defaultTools(courseTools)
                .build();
    }

    @Bean(name = "pdfChatClient")
    public ChatClient PdfChatClient(OpenAiChatModel openAiChatModel,VectorStore vectorStore){
        return ChatClient
                .builder(openAiChatModel)
                .defaultSystem("请根据上下文回答问题，遇到上下文没有的问题不要随意编造")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(getChatMemory()).build(),
                        QuestionAnswerAdvisor.builder(vectorStore)
                                .searchRequest(SearchRequest.builder()
                                        .similarityThreshold(0.4)
                                        .topK(2)
                                        .build())
                                .build()

                )
                .build();
    }

    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel openAiEmbeddingModel) {
        return SimpleVectorStore.builder(openAiEmbeddingModel).build();
    }

    @Bean
    public HttpMessageConverter<Object> createMappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter();
    }


}
